using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Slimes
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public static SpriteFont fontOne;
        GameState state;
        public long finalScore { get; set; }
        bool gameOver = false;
        GamePadState oldstate;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            state = new GameState(this);
            Components.Add(state);
            Components.Add(new Menu(this, state));


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Services.AddService(typeof(SpriteBatch), spriteBatch);
            Services.AddService(typeof(DisplayMode), GraphicsDevice.DisplayMode);

            fontOne = Content.Load<SpriteFont>("SpriteFont1");

            Slime.slimeTexture = Content.Load<Texture2D>("Slime");
            Slime.pixelTexture = Content.Load<Texture2D>("pixel");
            Menu.font = Content.Load<SpriteFont>("SpriteFont1");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Exits the game
            if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed && oldstate.Buttons.Back == ButtonState.Released) && gameOver)
                this.Exit();

            // Completes the game by pressing back or after 3 minutes
            if ((GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed && oldstate.Buttons.Back == ButtonState.Released) ||
                gameTime.TotalGameTime.TotalMinutes > 3 && !gameOver)
            {
                gameOver = true;
                Components.Clear();
                finalScore = state.money;
            }

            oldstate = GamePad.GetState(PlayerIndex.One);

            // TODO: Add your update logic here

            base.Update(gameTime);
        }

        protected override void OnExiting(object sender, EventArgs args)
        {
            finalScore = state.money;
            base.OnExiting(sender, args);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();

            if (!gameOver)
            {

                spriteBatch.DrawString(fontOne, "White: " + state.white, new Vector2(607, 30), Color.White);
                spriteBatch.DrawString(fontOne, "Purple: " + state.purple, new Vector2(600, 60), Color.Purple);
                spriteBatch.DrawString(fontOne, "Blue: " + state.blue, new Vector2(626, 90), Color.Blue);
                spriteBatch.DrawString(fontOne, "Green: " + state.green, new Vector2(608, 120), Color.Green);
                spriteBatch.DrawString(fontOne, "Yellow: " + state.yellow, new Vector2(598, 150), Color.Yellow);
                spriteBatch.DrawString(fontOne, "Red: " + state.red, new Vector2(637, 180), Color.Red);

                spriteBatch.DrawString(fontOne, "" + state.money, new Vector2(250, 10), Color.White);

            }
            else
            {
                spriteBatch.DrawString(fontOne, "Game Over", new Vector2(250, 175),
                                        Color.White, 0, Vector2.One, 2f, SpriteEffects.None, 1);
                spriteBatch.DrawString(fontOne, "" + finalScore, new Vector2(450 - fontOne.MeasureString("" + finalScore).X * 1.5f, 275),
                                        Color.White, 0, Vector2.One, 2f, SpriteEffects.None, 1);
            }

            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}
